package org.patto.main.api.impl;

import org.apache.commons.lang3.StringUtils;
import org.patto.common.core.api.BaseApiService;
import org.patto.common.core.api.BaseResponse;
import org.patto.main.api.UserInfoService;
import org.patto.main.api.dto.req.UserResDTO;
import org.patto.main.api.impl.entity.SysUser;
import org.patto.main.api.impl.mapper.SysUserMapper;
import org.patto.common.core.utils.MeiteBeanUtils;
import org.patto.common.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author songchuanfu
 * @ClassName UserInfoServiceImpl
 * @qq 18050164315
 * @addres 1802284273@qq.com
 * @微信: wx-suagcf
 */
@RestController
public class UserInfoServiceImpl extends BaseApiService<UserResDTO> implements UserInfoService {
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 保证接口的安全性
     *
     * @param token
     * @return
     */
    @Override
    public BaseResponse<UserResDTO> getByTokenUserInfo(String token) {
        /**
         * 根据用户的token获取redis中userid
         * 在根据userid查询db信息
         */
        if (StringUtils.isEmpty(token)) {
            return setResultError("token is null");
        }
        //根据用户的token获取redis中userid
        String redisUserId = RedisUtil.getString(token);
        if (StringUtils.isEmpty(redisUserId)) {
            return setResultError("token is error");
        }
        Integer userId = Integer.parseInt(redisUserId);
        SysUser sysUser = sysUserMapper.selectById(userId);
        if (sysUser == null) {
            return setResultError();
        }
        // 获取db对象 将db中转换 dto
        UserResDTO userResDto = MeiteBeanUtils.doToDto(sysUser, UserResDTO.class);
        return setResultSuccessData(userResDto);
    }
}
